using System;
using System.Collections.Generic;
using System.Text;

namespace Dogfight2008
{
  public class Collision<T>
  {
    public Collision(T one, T two)
    {
      One = one;
      Two = two;
    }

    public bool Match(T one, T two)
    {
      return (One.Equals(one) && two.Equals(Two)) || (One.Equals(two) && one.Equals(Two));
    }

    public override int GetHashCode()
    {
      unchecked { return One.GetHashCode() + Two.GetHashCode(); };
    }

    public readonly T One, Two;

    public override string ToString()
    {
      return string.Format("Collision between {0} and {1}", One, Two);
    }

    public override bool Equals(object obj)
    {
      Collision<T> rhs = obj as Collision<T>;
      if (rhs == null)
        return false;

      return One.Equals(rhs.One) && Two.Equals(rhs.Two);
    }
  }

  public class Box2dCollisionDetector<T>
  {
    List<Box2d> boxes = new List<Box2d>();
    List<T> ids = new List<T>();

    public void AddBox(Box2d box2d, T t)
    {
      boxes.Add(box2d);
      ids.Add(t);
    }

    public List<Collision<T>> Detect()
    {
      List<Collision<T>> colls = new List<Collision<T>>();

      // brute force it for now ...
      for (int i = 0; i < boxes.Count; i++)
        for (int j = i + 1; j < boxes.Count; j++)
          if (i != j)
            if (boxes[i].Intersects(boxes[j]))
              colls.Add(new Collision<T>(ids[i], ids[j]));

      return colls;
    }
  }
}
